package com.example.demo.leetcode;

import org.junit.Test;

import java.util.*;

// list.sort(Collections.reverseOrder());
public class leetcode_1636 {

    @Test
    public void test() {
        int[] arr = {1, 1, 2, 2, 2, 3};
        int[] res = frequencySort(arr);
        System.out.println(Arrays.toString(res));
    }

    public int[] frequencySort(int[] nums) {
        Map<Integer, Integer> resMap = new HashMap<>();
        List<Integer> numsList = new ArrayList<>();

        //统计次数
        for (int curr : nums) {
            numsList.add(curr);
            resMap.put(curr, resMap.getOrDefault(curr, 0) + 1);
        }

        //按规则排序
        numsList.sort((a, b) -> {
            int aCount = resMap.get(a);
            int bCount = resMap.get(b);
            if (aCount == bCount) {
                return b - a;
            }
            return aCount - bCount;
        });

        return numsList.stream().mapToInt(Integer::intValue).toArray();
    }
}
